package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.enums.DingType;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.ding.DingBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DingConsumer {

    public static Logger log = LoggerFactory.getLogger(DingConsumer.class);

    @Autowired
    DingBiz dingBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.ding_warning_queue, durable = "true"), exchange = @Exchange(value = MqConstants.ding_warning_exchange, durable = "true", type = "topic"), key = MqConstants.ding_warning_key))
    public void DingWarning(String msg) {
        JsonObject returnData = GsonUtils.fromJson(msg, JsonObject.class);
        log.info("[mq收到钉钉报警消息msg：{}]", msg);
        Integer type = returnData.get("type").getAsInt();
        String text = returnData.get("text").getAsString();
        if (type.equals(DingType.cms.getCode())) {
            dingBiz.dingNotify(DingType.cms.getMsg(), text);
        }
        if (type.equals(DingType.api.getCode())) {
            dingBiz.dingNotify(DingType.api.getMsg(), text);
        }
        if (type.equals(DingType.android.getCode())) {
            dingBiz.atNotify(DingType.android.getMsg(), text, "15201108566");
        }
    }
}
